package com.inossem.warehousestorage_inossem.activity.change_storage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.extra.ChangeStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.changeStorage.ChangeStorageSelectMaterielAdapter;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageSelectMaterielBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.changeStorage.ChangeStorageSelectMaterielRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;


public class ChangeStorageSelectMaterielActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView materielList;

    private ChangeStorageSelectMaterielAdapter adapter;

    private String mMaterielCode, mFactoryCode, mInventoryAddressCode, mSpecialInventory;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_in_storage_other_select_supplier);
        initView(ChangeStorageSelectMaterielActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(ChangeStorageSelectMaterielActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        materielList = (PullableListView) activity.findViewById(R.id.supplierList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.supplierListRefresh);
    }

    private void initData(Activity activity) {
        title.setText(R.string.activity_change_storage_select_materiel_title);
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_change_storage_select_material_details), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        materielList.setPullUpFlag(false);
        materielList.setPullDownFlag(false);
        getExtra(activity);
        mMaterielCode = "";
        addMaterielListData(activity, new ArrayList<>(), materielList, true);
        setmScanListener((String scanResult, int length) -> {
            mMaterielCode = QRCodeUtils.DecryptQRCode(scanResult);
            getData(activity, mMaterielCode);
        }, QRCodeConstant.TYPE_BATCH, QRCodeConstant.TYPE_MATERIEL_CODE, QRCodeConstant.TYPE_PURCHASE_ORDER, QRCodeConstant.TYPE_POSITION);
    }

    private void getExtra(Activity activity) {
        mFactoryCode = activity.getIntent().getStringExtra(ChangeStorageExtra.FACTORY_CODE);
        mInventoryAddressCode = activity.getIntent().getStringExtra(ChangeStorageExtra.INVENTORY_ADDRESS_CODE);
        mSpecialInventory = activity.getIntent().getStringExtra(ChangeStorageExtra.SPECIAL_INVENTORY);
    }

    private void addMaterielListData(Activity activity, List<ChangeStorageSelectMaterielBean.MaterielListBean> materielList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new ChangeStorageSelectMaterielAdapter(activity, materielList, mMaterielCode, new ChangeStorageSelectMaterielActivity.ChangeStorageOtherSelectSpecialInventoryListener() {
                @Override
                public NoDoubleClickListener getNoDoubleClickListener(EditText materielCode) {
                    NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            if (TextUtils.isEmpty(materielCode.getText().toString())) {
                                Utils.middleToast(activity, getResources().getString(R.string.activity_change_storage_select_the_material_encoding_must_not_be_empty));
                            } else {
                                Utils.closeSoftKeyboard(activity, v);
                                mMaterielCode = materielCode.getText().toString();
                                getData(activity, mMaterielCode);
                            }
                        }
                    };
                    return noDoubleClickListener;
                }

                @Override
                public NoDoubleClickListener select(String code, String description  ,String matId) {
                    NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra(ChangeStorageExtra.MATERIEL_CODE, code);
                            intent.putExtra(ChangeStorageExtra.MATERIEL_DESCRIPTION, description);
                            intent.putExtra(ChangeStorageExtra.MATERIEL_ID, matId);
                            activity.setResult(ChangeStorageMaterielDetailActivity.SELECT_MATERIEL, intent);
                            activity.finish();
                        }
                    };
                    return noDoubleClickListener;
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Activity activity, String condition) {
        ChangeStorageSelectMaterielRequest.request(activity, condition, mFactoryCode, mInventoryAddressCode, mSpecialInventory, new GsonBeanListener<ChangeStorageSelectMaterielBean>() {
            @Override
            public void onSuccess(ChangeStorageSelectMaterielBean bean) {
                if (bean.getBody().isEmpty()) {
                    Utils.middleToast(activity, getResources().getString(R.string.activity_change_storage_materiel_list_undetected_data));
                }
                addMaterielListData(activity, bean.getBody(), materielList, true);
            }
        });
    }

    public interface ChangeStorageOtherSelectSpecialInventoryListener {
        NoDoubleClickListener getNoDoubleClickListener(EditText specialInventoryCode);

        NoDoubleClickListener select(String specialInventoryCode, String specialInventoryDescription , String matId);
    }

}
